<template>
  <div>
    <el-form :model="studio" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item  prop="studioname" style='width:80%'>
        <el-input v-model="studio.studioname" placeholder="请输入画室名称" ></el-input>
      </el-form-item>
      <el-form-item  prop="provice" style='width:80%'>
        <el-select placeholder="请选择画室所在地区" v-model="studio.provice" style="width:100%">
          <el-option v-for="province in provinceList"
                    :key="province.province"
                    :value="province.province"
                    :label="province.province">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  prop=“contact“ style='width:80%'>
        <el-input v-model="studio.contact" placeholder="请填写联系人"></el-input>
      </el-form-item>
      <el-form-item   prop="phone" style='width:80%'>
        <el-input v-model="studio.phone" placeholder="请填写联系人手机号码"></el-input>
      </el-form-item>
      <el-form-item   prop="email" style='width:80%'>
        <el-input v-model="studio.email" placeholder="请填写联系emial"></el-input>
      </el-form-item>
      <el-form-item prop="url" style='width:80%'>
        <el-row style='width:100%'>
      <el-col :span="16">
         <el-input v-model="studio.url" placeholder="请上传学生名单"></el-input>
      </el-col>
      <el-col :span="8">
        <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-exceed="handleExceed"
          :show-file-list='false'
          :on-success="upok"
          :file-list="fileList">
          <el-button  type="primary">选择文件</el-button>
        </el-upload>
       </el-col>
       </el-row>
      </el-form-item>
      <el-form-item  prop="submit" style='width:80%'>
        <el-button type="primary" style="width:100%" @click="submitForm('ruleForm')">确认修改</el-button>

      </el-form-item>
    </el-form>

   
  </div>

</template>
<script>
  import provinceCodeList from "../../utils/provinceCode";
  //这里要俺需要引入，我不是一个对象
  import {isvalidPhone,isvalidEmail} from '../../api/validata'
  //定义一个全局的变量，谁用谁知道
  var validPhone=(rule, value,callback)=>{
      if (!value){
          callback(new Error('请输入电话号码'))
      }else  if (!isvalidPhone(value)){
        callback(new Error('请输入正确的11位手机号码'))
      }else {
          callback()
      }
  }

  export default {
    props:{studio:{type:Object ,default:{
          studioname:'',
          provice:'',
          contact:'',
          phone:'',
          email:'',
          url:''
    }}}, 
    data() {
      return {
        provinceList: provinceCodeList,
        rules: {
          studioname: [
            { required: true, message: '输入画室名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          provice: [
            { required: true, message: '输入省份', trigger: 'blur' },
          ],
          contact: [
            { required: true, message: '输入联系老师', trigger: 'blur' },
          ],
          phone: [
            { required: true, trigger: 'blur', validator: validPhone }//这里需要用到全局变量
          ],
          email: [
            { required: true, message: '请输入email', trigger: 'blur' },//这里需要用到全局变量
            { type: 'email', message: 'email格式错误,请输入正确的email', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      upok(response, file, fileList){
        
        this.studio.url=file.name
        console.log(this.studio,file.name,this.studio.url)
        console.log(file,response,fileList)
        console.log('文件上传至服务器1')
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');//这里就是符合规则，然后去调对应的接口
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }

</script>